<?php
/**
 * Created by PhpStorm.
 * User: 我想有个家
 * Date: 2019/5/24
 * Time: 17:46
 */

namespace App\Controller\admin;


use App\Model\AppVersion;
use App\Validate\AppVersionValidate;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class AppController
 * @package App\Controller\admin
 * @Controller(prefix="a/app")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class AppController extends AdminBase {

    /**
     * 列表
     * @RequestMapping(path="lists",methods="get")
     */
    public function getLists() {
        $size = $this->get('size');
        $type = $this->get('type');
        $time = $this->get('time');
        $size = pageSizeVerify($size);
        $condition = [];
        $time = explode(',', $time);
        if (count($time) == 2) {
            $date[0] = $time[0] . ' 00:00:00';
            $date[1] = $time[1] . ' 23:59:59';
            $condition[] = ['create_time', 'between', $date];
        }
        switch ($type) {
            case 'android':
                $condition[] = ['platform', '=', 1];
                break;
            case 'ios':
                $condition[] = ['platform', '=', 2];
                break;
        }
        $result = AppVersion::query()
            ->where($condition)
            ->orderBy('id', 'desc')
            ->select('id', 'version', 'log', 'url', 'must', 'platform', 'create_time')
            ->paginate($size);
        return success(paginate($result));
    }

    /**
     * 添加
     * @RequestMapping(path="version",methods="post")
     * @return array
     */
    public function postVersion() {
        $validate = $this->di->get(AppVersionValidate::class);
        $validate->check($validate->add());
        $param['version'] = $this->body('version');
        $param['log'] = $this->body('log');
        $param['url'] = $this->body('url');
        $param['must'] = $this->body('must');
        $param['platform'] = $this->body('platform');
        $res = AppVersion::query()->insertGetId($param);
        return $res ? success() : error();
    }

    /**
     * 删除
     * @return array
     * @RequestMapping(path="version",methods="delete")
     */
    public function deleteVersion() {
        $id = $this->body('id');
        if ($id < 1) {
            return error('参数错误');
        }
        $res = AppVersion::query()->where('id', $id)->delete();
        return $res ? success() : error();
    }
}
